package GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.sf.feeling.swt.win32.extension.shell.Windows;

import backend.Task;


/**This class create the pop up windows as when required.
 * 3 actions are supported, ignore, updates and
 * updates with specified data.
 * @author Defeng
 *
 */
class InputDialog extends Dialog {
	
private String taskDescription;
private String taskAttributes;
private String defaultFields;

private String returnValue;

/**Constructor 1
 * @param parent
 */
public InputDialog(Shell parent) {
	this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
/**Constructor 2 that also takes in data for manupilation
 * @param parent
 * @param aTask
 */
public InputDialog(Shell parent, Task aTask) {
  	this(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
/**Constructor 3 that is being called by constructor 1 and 2.
 * has the 3 methods to update fields in this class. Also sets 
 * the title of the pop up
 * @param parent
 * @param style
 */
public InputDialog(Shell parent, int style) {
	super(parent, style);
	setText("Reminders");
	setTaskDescription("~Task description here~");
	setDefaultFields("~Default field for user here~");
	setTaskAttributes("~Task attributes here");
}
  
/**Prepares the shell for the popup, centralize it and then
 * show it to the user.	Handles closing of the popup. 
 * Also allows popup to be above all windows.
 * @return
 */
public String open() {
	Display display = getParent().getDisplay();
    Shell shell = new Shell(getParent(), SWT.TITLE);
    Windows.setWindowAlwaysOnTop(shell.handle, true);
    //Windows.setWindowAlwaysOnTop(shell.handle, false);
    
    //centralize the popup
    shell.setSize(376,160);
    Rectangle bounds = display.getBounds();
    Rectangle rect = shell.getBounds();
    int x = bounds.x + (bounds.width - rect.width) / 2;
    int y = bounds.y + (bounds.height - rect.height) / 2;
    shell.setLocation(x, y);
    //populate the fields
    shell.setText(getText());
    createContents(shell);
    //ready popup for display
    shell.pack();
    shell.open();
    //w8 for popup to be closed
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    return returnValue;
}
  
public String getTaskDescription() {
 	return taskDescription;
}
public void setTaskDescription(String str) {
	this.taskDescription = str;
}	
public String getTaskAttributes() {
	return taskAttributes;
}
public void setTaskAttributes(String str) {
	this.taskAttributes = str;
}
public String getDefaultFields() {
	return defaultFields;
}
public void setDefaultFields(String str) {
	this.defaultFields = str;
}

/**Draws and setup the positioning and layout of the pop up window
 * and fills the fields with data
 * @param shell
 * @return void
 */
private void createContents(final Shell shell) {
	shell.setLayout(new GridLayout(3, true));
	
	Label taskDescription = new Label(shell, SWT.NONE);
	taskDescription.setFont(SWTResourceManager.getFont("Lucida Grande", 12, SWT.BOLD));
	taskDescription.setAlignment(SWT.CENTER);
	taskDescription.setText(this.taskDescription);
	
	GridData data = new GridData();
	data.horizontalAlignment = SWT.CENTER;
	data.horizontalSpan = 3;
	taskDescription.setLayoutData(data);
	
	Label modPostpone = new Label(shell, SWT.NONE);
	modPostpone.setText("Postpone/Modify Task:");
	    
	    new Label(shell, SWT.NONE);
	    
	    Label taskAttributes = new Label(shell, SWT.NONE);
	    taskAttributes.setText(this.taskAttributes);
	    				
	    final Text userInput = new Text(shell, SWT.BORDER);
	    data = new GridData(GridData.FILL_HORIZONTAL);
	    data.horizontalSpan = 3;
	    userInput.setLayoutData(data);
	    userInput.setText(defaultFields);
	    userInput.setSelection(defaultFields.length());
	    
	    new Label(shell, SWT.NONE);
	    new Label(shell, SWT.NONE);
	    new Label(shell, SWT.NONE);
	 
	    Button ignore = new Button(shell, SWT.PUSH);
	    ignore.setText("Ignore");
	data = new GridData(GridData.FILL_HORIZONTAL);
	ignore.setLayoutData(data);
	ignore.addSelectionListener(new SelectionAdapter() {
	  public void widgetSelected(SelectionEvent event) {
	    returnValue = "Ignore";
	    shell.close();
	  }
	});
	    
	Button postpone = new Button(shell, SWT.PUSH);
	postpone.setText("Postpone");
	data = new GridData(GridData.FILL_HORIZONTAL);
	postpone.setLayoutData(data);
	postpone.addSelectionListener(new SelectionAdapter() {
	  public void widgetSelected(SelectionEvent event) {
		returnValue = userInput.getText();
	    shell.close();
	  }
	});
	
	Button done = new Button(shell, SWT.PUSH);
	done.setText("Done");
	data = new GridData(GridData.FILL_HORIZONTAL);
	done.setLayoutData(data);
	done.addSelectionListener(new SelectionAdapter() {
	  public void widgetSelected(SelectionEvent event) {
		returnValue = "Done"; 
	    shell.close();
	  }
	});
	//shell.setDefaultButton(done); 
}
 
/**main method used for testing without any parent shell
 * @param args
 */
/* 
public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	
	InputDialog dlg = new InputDialog(shell);
	String output = dlg.open();
	
	if (output != null) {
	  System.out.println(output);
	}
	display.dispose();
}
*/
  
}



